#include "keyboard.h"

DEFINE_SINGLETON(Keyboard);

Keyboard::Keyboard(const string& name, ClassId* classId):
Instance(name, classId)
{
}

Keyboard::~Keyboard()
{
}

void Keyboard::startTextInput()
{
    SDL_StartTextInput();
}

void Keyboard::stopTextInput()
{
    SDL_StopTextInput();
}

void Keyboard::handleKeyEvent(SDL_KeyboardEvent* event, float dt)
{
    assert(event);

    Window* window = Window::getFromId(event->windowID);
    Keyboard* keyboard = Keyboard::getActive();

    if(!keyboard) {
        return;
    }
    switch (event->type) {
        case SDL_KEYDOWN:
            keyboard->onKeyDown(window, event->keysym, event->repeat, dt);
            break;
        case SDL_KEYUP:
            keyboard->onKeyUp(window, event->keysym, dt);
            break;
    }
}

void Keyboard::onKeyDown(Window* window, SDL_Keysym keysym, bool isRepeat, float dt)
{
    if(keysym.sym == SDLK_ESCAPE) {
        exit (0);
    }
}

void Keyboard::onKeyUp(Window* window, SDL_Keysym keysym, float dt)
{
}

void Keyboard::handleTextInputEvent(SDL_TextInputEvent* event, float dt)
{
    assert(event);

    Window* window = Window::getFromId(event->windowID);
    Keyboard* keyboard = Keyboard::getActive();

    if(!keyboard)
        return;
    keyboard->onTextInput(window, event->text);
}

void Keyboard::onTextInput(Window* window, char text[SDL_TEXTINPUTEVENT_TEXT_SIZE])
{
}

bool Keyboard::load(DataStream* ds)
{
    INSTANCE_LOAD(ds);

    return true;
}

bool Keyboard::save(DataStream* ds)
{
    INSTANCE_SAVE(ds);

    return true;
}
